package com.google.code.swingonfire.component.calendar;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;

import com.google.code.swingonfire.component.calendar.JMonthTableModel.DayOfWeekEntry;

public class JMonthTableDayOfWeekRenderer implements TableCellRenderer
{

    private final JLabel label;
    private final Font defaultFont;
    private final Color defaultForeground;

    public JMonthTableDayOfWeekRenderer()
    {
        super();

        label = new JLabel();

        prepare(label);

        defaultFont = label.getFont();
        defaultForeground = label.getForeground();
    }

    private void prepare(JLabel label)
    {
        label.setHorizontalAlignment(SwingConstants.CENTER);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column)
    {
        if (value == null)
        {
            label.setText("");
            label.setFont(defaultFont);
            label.setForeground(defaultForeground);
        }
        else
        {
            DayOfWeekEntry entry = (DayOfWeekEntry) value;

            label.setText(entry.getDayOfWeekName());

            Font font = entry.getDayOfWeekFont(defaultFont);

            label.setFont((font != null) ? font : defaultFont);

            Color foreground = entry.getDayOfWeekForeground(defaultForeground);

            label.setForeground((foreground != null) ? foreground : defaultForeground);
        }

        return label;
    }

}
